<?php

class User_collectController extends BaseController {
	public function __construct() {
		if (Doit::getActionName() != 'getByUuid') {
			parent::__construct();
		}
	}

	public function isCollectAction() {
		$requestData = [];
		$requestData['type'] = intval(Request::getParams('type'));
		$requestData['type_id'] = Request::getParams('typeId');
		Response::ajax(200, 'success',
			self::model('user_collect')->getCollectIds($this->user_info['uid'], $requestData['type'], $requestData['type_id']));

	}

	public function addAction() {
		if (!$userInfo = self::model('user')->findById($this->user_info['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$requestData = [];
		$requestData['type'] = intval(Request::getParams('type'));
		$requestData['type_id'] = intval(Request::getParams('typeId'));
		if (!$requestData['type'] || !$requestData['type_id']) {
			Response::ajax(NORMAL_ERROR, '参数错误,收藏失败');
		}
		if (self::model('user_collect')->isCollect($this->user_info['uid'], $requestData['type_id'], $requestData['type'])) {
			Response::ajax(200, '您已收藏');
		}
		$requestData['add_time'] = time();
		$requestData['day'] = date('Y-m-d');
		$requestData['uid'] = $this->user_info['uid'];
		try {
			if (self::model('user_collect')->insert($requestData)) {
				if ($requestData['type'] == user_collectModel::DYNAMIC_TYPE) {
					self::model('dynamic')->execute("update wf_dynamic set collect_total=collect_total+1 where dynamic_id={$requestData['type_id']}");
					if (!$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $requestData['type_id'])) {
						Response::ajax(NORMAL_ERROR, '参数有误');
					}
					self::model('user')->execute("update wf_user set collect_dynamic_total=collect_dynamic_total+1 where uid={$this->user_info['uid']}");
					user_notifyModel::addNotify($dynamicInfo['uid'], notifyModel::TYPE_COLLECT, $requestData['type_id'], $userInfo, $dynamicInfo['title']);

				} elseif ($requestData['type'] == user_collectModel::DYNAMIC_TYPES) {
					self::model('dynamic')->execute("update wf_dynamic set collect_total=collect_total+1 where dynamic_id={$requestData['type_id']}");
					if (!$dynamicInfo = self::model('dynamic')->getOne('dynamic_id=?', $requestData['type_id'])) {
						Response::ajax(NORMAL_ERROR, '参数有误');
					}
					self::model('user')->execute("update wf_user set collect_dynamic_total=collect_dynamic_total+1 where uid={$this->user_info['uid']}");
					user_notifyModel::addNotify($dynamicInfo['uid'], notifyModel::TYPE_COLLECT, $requestData['type_id'], $userInfo, $dynamicInfo['title']);

				} elseif ($requestData['type'] == user_collectModel::GOODS_TYPE) {
					self::model('user')->execute("update wf_user set collect_goods_total=collect_goods_total+1 where uid={$this->user_info['uid']}");
				}
				Response::ajax(200, '收藏成功');
			}
		} catch (Exception $ex) {
			Log::write('添加收藏有误:' . $ex->getTraceAsString() . 'request:' . json_encode($requestData, 320), 'WARN');
		}
		Response::ajax(NORMAL_ERROR, '收藏失败,请稍后重试');
	}

	public function removeAction() {
		$requestData = [];
		$requestData['type'] = intval(Request::getParams('type'));
		$requestData['type_id'] = Request::getParams('typeId');
		if (!$requestData['type'] || !$requestData['type_id']) {
			Response::ajax(NORMAL_ERROR, '参数错误,收藏失败');
		}
		try {
			if (self::model('user_collect')->deleteByIds($this->user_info['uid'], $requestData['type'], $requestData['type_id'])) {
				$count = self::model('user_collect')->getCount($this->user_info['uid'], $requestData['type']);
				if ($requestData['type'] == user_collectModel::DYNAMIC_TYPE) {
					self::model('user')->execute("update wf_user set collect_dynamic_total={$count} where uid={$this->user_info['uid']}");
					self::model('dynamic')->execute("update wf_dynamic set collect_total=collect_total-1 where dynamic_id={$requestData['type_id']}");
				} elseif ($requestData['type'] == user_collectModel::GOODS_TYPE) {
					self::model('user')->execute("update wf_user set collect_goods_total={$count} where uid={$this->user_info['uid']}");
				}
				Response::ajax(200, '取消收藏成功');
			}
		} catch (Exception $ex) {
			Log::write('取消收藏有误:' . $ex->getTraceAsString() . 'request:' . json_encode($requestData, 320), 'WARN');
		}
		Response::ajax(NORMAL_ERROR, '取消失败,请稍后重试');
	}

	public function listAction() {
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 10));
		$type = intval(Request::getParams('type', 2));
        $category_id = Request::getParams('category_id');
        $category_list=self::model('goods_category')->fields(['category_id','name'])->getAll('parent_id=?',0);
		Response::ajax(200, 'success', ['collect_list'=>self::model('user_collect')
            ->getList($this->user_info['uid'], $type, $page, $pageSize,$category_id),'category_list'=>$category_list]);
	}

	public function getByUuidAction() {
		$uid = intval(Request::getParams('uid'));
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 10));
		$type = intval(Request::getParams('type', 1));
        Response::ajax(200, 'success', self::model('user_collect')
				->getList($uid, $type, $page, $pageSize));
	}

}